package com.mxprojects.views
{
	import com.mxprojects.constructors.ButtonLarge;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.constructors.SimpleTextField;
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.io.UserControl;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;

	public class ForgotPassword extends LoginDialog
	{		
		private static const RESET_PASSWORD:String = "resetPassword";
		private static const VERIFY_RESET:String = "verifyReset";
		
		private static var nextY:int;
		private static var emailInput:InputConstructor;
		private static var submitButton:ButtonLarge;
		
		public function ForgotPassword(){}
		
		public static function show():void {
			//this will keep holding the login window in place and just replace the content
			loginDialog.title = Language.main(Word.MESSAGE_FORGOT_PASSWORD);
			
			//remove the login stuff
			loginDialog.removeContent(usernameInput, false);
			loginDialog.removeContent(passwordInput, false);
			loginDialog.removeContent(forgotPassword, false);
			loginDialog.removeContent(loginButton, false);
			
			//setup where stuff goes			
			nextY = Math.round(loginDialog.titleText.height + (Config.TEXT_PADDING * 2));
			
			//add the message
			var textBlock:SimpleTextField = loginDialog.createTextBlock(Language.main(Word.MESSAGE_RESET_PASSWORD));
			nextY += textBlock.height;
			
			//add the email input
			emailInput = new InputConstructor(Language.main(Word.EMAIL));
			emailInput.x = Math.round(loginDialog.width/2 - emailInput.width/2);
			emailInput.y = nextY;
			nextY += emailInput.height + Config.TEXT_PADDING;
			loginDialog.addContent(emailInput);
			
			//create the submit button
			submitButton = loginDialog.createActionButton(Language.main(Word.SUBMIT), sendEmail);
			submitButton.y = nextY;
			
			//listen to the keyboard
			listen();
		}
		
		public static function reset(email:String, key:String):void {
			//check to make sure everything is a-ok and send it along
			var data:Object = new Object();
			data.action = VERIFY_RESET;
			data.email = email;
			data.key = key;
			
			var server:ServerHandler = new ServerHandler();
			server.transmitData("forgot-password.php", data);
			server.listenForData(handleVerify);
		}
		
		private static function listen():void {
			loginDialog.stage.addEventListener(KeyboardEvent.KEY_DOWN, check, false, 0, true);
			loginDialog.stage.focus = emailInput.inputBox;
			emailInput.inputBox.setSelection(0, emailInput.inputBox.text.length);
		}
		
		private static function stopListening():void {
			loginDialog.stage.removeEventListener(KeyboardEvent.KEY_DOWN, check);
		}
		
		private static function check(event:KeyboardEvent):void {
			if(event.keyCode == Keyboard.ENTER){
				sendEmail();
			}
			
			if(event.keyCode == Keyboard.ESCAPE){
				stopListening();
				close();
			}
		}
		
		private static function sendEmail(event:MouseEvent = null):void {
			var dataToSend:Object = new Object();
			dataToSend.email = emailInput.getValue();
			dataToSend.action = RESET_PASSWORD;
			dataToSend.subject = Language.main(Word.MESSAGE_PASSWORD_RESET_REQUEST);
			dataToSend.message = Language.main(Word.MESSAGE_FORGOT_PASSWORD_EMAIL);
			dataToSend.installDirectory = Config.INSTALL_DIRECTORY;
			dataToSend.boardName = Config.BOARD_NAME;
			dataToSend.boardEmail = Config.BOARD_EMAIL;
			
			var serverHandler:ServerHandler = new ServerHandler();
			serverHandler.transmitData("forgot-password.php", dataToSend);
			serverHandler.listenForData(processData);
			
			//diable the submit button
			submitButton.enabled = false;
		}
		
		private static function processData(event:CustomEvent):void {
			//handle the reply from the server
			var data:Object = event.getServerData();
			
			switch(data.output){
				case "emailError":
					DefaultDialog.show(Language.main(Word.ERROR_VALID_EMAIL), false);
					submitButton.enabled = true;
				break;
				
				case "emailNotFound":
					DefaultDialog.show(Language.main(Word.ERROR_EMAIL_NOT_FOUND), false);
					submitButton.enabled = true;
				break;
				
				case "mysqlError":
					DefaultDialog.show(Language.main(Word.ERROR_MYSQL) + "<br>" + data.mysqlError, false);
					submitButton.enabled = true;
				break;
				
				case "success":
					close();
					DefaultDialog.show(Language.main(Word.MESSAGE_PASSWORD_EMAIL_SENT), true, Word.SUCCESS);
				break;
			}
		}
		
		private static function handleVerify(event:CustomEvent):void {
			//handle the reply from the server
			var data:Object = event.getServerData();
			
			switch(data.output){
				case "keyMatchError":
					DefaultDialog.show(Language.main(Word.ERROR_EMAIL_KEY_MISMATCH), false);
				break;
				
				case "mysqlError":
					DefaultDialog.show(Language.main(Word.ERROR_MYSQL) + "<br>" + data.mysqlError, false);
				break;
				
				case "success":
					DefaultDialog.show(Language.main(Word.MESSAGE_PASSWORD_CHANGED), true, Word.SUCCESS);
					UserControl.login(data.username, data.password, true);
					//DeepLinking.setLink(Word.PROFILE, DeepLinking.createLinkArray(-1));
				break;
			}
		}
	}
}